﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.Origins;
using System.Data;
using System.Data.SqlClient;
using Enums;
using Enums.Sys;
using Common;
using DAL.DBUtility;
namespace DAL.Origins
{
    public class ApplyCodeDAL
    {

        //获取申请单
        public List<ApplyCodeModel> Getlist(string str)
        {
            string sql = @"select oa.*,oe.EnterpriseName,oe.Code,
case oa.[Status] when 0 then '待审核..' when 1 then '审核成功' else '审核失败' end as [StatusRemark]
from Origins_ApplyCode oa
left join Organization_Enterprise oe on oa.EnterPriseId=oe.ID where oa.ViewDel=@ViewDel ";
            sql += str;
            SqlParameter[] parameters = {
                                      new SqlParameter("@ViewDel",EnumManagement.GetEnumValue(SysEnum.NoDele)),
                                        // new SqlParameter("@EnterPriseId",enterId),
                                      };

            return ListDataSet.DataSetToIList<ApplyCodeModel>(DbHelperSQL.Query(sql, parameters).Tables[0]).ToList();
        }
        public List<ApplyCodeModel> Getlist1(int enterId, int roleId)
        {
            string sql = @"select oa.*,oe.EnterpriseName,oe.Code from Origins_ApplyCode oa
left join Organization_Enterprise oe on oa.EnterPriseId=oe.ID   where oa.ViewDel=@ViewDel ";
           // if (roleId != 1) sql += " and oa.EnterPriseId=@EnterPriseId";
            SqlParameter[] parameters = {
                                      new SqlParameter("@ViewDel",EnumManagement.GetEnumValue(SysEnum.NoDele)),
                                         new SqlParameter("@EnterPriseId",enterId),
                                      };

            return ListDataSet.DataSetToIList<ApplyCodeModel>(DbHelperSQL.Query(sql, parameters).Tables[0]).ToList();
        }
        //新增一个申请
        public ResultEntity AddApply(ApplyCodeModel model)
        {
            string sql = @" insert into Origins_ApplyCode(EnterPriseId,ApplyNum,CreateLoginID,CreateLoginName,CreateDate,ViewDel,Remark)
 
       values(@EnterPriseId,@ApplyNum,@CreateLoginID,@CreateLoginName,@CreateDate,@ViewDel,@Remark)";
            SqlParameter[] parameters = { new SqlParameter("@EnterPriseId",model.EnterPriseId),
                                      new SqlParameter("@ApplyNum",model.ApplyNum),
                                         new SqlParameter("@CreateLoginID",model.CreateLoginID),
                                      new SqlParameter("@CreateLoginName",model.CreateLoginName),
                                      new SqlParameter("@CreateDate",model.CreateDate),
                                      new SqlParameter("@ViewDel",EnumManagement.GetEnumValue(SysEnum.NoDele)),
                                     new SqlParameter("@Remark",model.Remark),
      };
            return DbHelperSQL.CommonResult(DbHelperSQL.ExecuteSql(sql, parameters));
        }
        //编辑
        public ResultEntity UpdateApply(ApplyCodeModel model)
        {
            string sql = @"  update Origins_ApplyCode set ApplyNum=@ApplyNum,ChangeLoginID=@ChangeLoginID,ChangeLoginName=@ChangeLoginName,ChangeDate=@ChangeDate,Remark=@Remark
  where ID=@ID";
            SqlParameter[] parameters = { 
                                            
                                
                                      new SqlParameter("@ApplyNum",model.ApplyNum),
                                      new SqlParameter("@ChangeLoginID",model.ChangeLoginID),
                                      new SqlParameter("@ChangeLoginName",model.ChangeLoginName),
                                      new SqlParameter("@ChangeDate",model.ChangeDate),
                                            new SqlParameter("@Remark",model.Remark),
                                             new SqlParameter("@ID",model.ID),
                                   
      };
            return DbHelperSQL.CommonResult(DbHelperSQL.ExecuteSql(sql, parameters));
        }

        public ApplyCodeModel GetModel(int Id)
        {

            string sql = @"select * from Origins_ApplyCode where ID=@ID";
            SqlParameter[] parameters = { 
                                        new SqlParameter("@ID",Id)
                                        };
            SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, parameters);
            ApplyCodeModel model = new ApplyCodeModel();
            if (sdr.Read())
            {
                model = CommonModel(sdr);
            }
            sdr.Close();
            return model;

        }

        //更改提交状态
        public ResultEntity UpdateStaus(int Id) {//1已审核
            string sql = @"update Origins_ApplyCode set Status=1 where ID=@ID";
            SqlParameter[] parameters = { 
                                            //  new SqlParameter("@Status",status),
                                        new SqlParameter("@ID",Id)
                                        };
            return DbHelperSQL.CommonResult(DbHelperSQL.ExecuteSql(sql, parameters));
        }
        private ApplyCodeModel CommonModel(SqlDataReader sdr)
        {
            ApplyCodeModel model = new ApplyCodeModel();
            model.ID = PageValidate.ConvertToTrimInt(sdr["ID"]);
            model.EnterPriseId = PageValidate.ConvertToTrimInt(sdr["EnterPriseId"]);
            model.ApplyNum = PageValidate.ConvertToTrimInt(sdr["ApplyNum"]);
            model.Status = PageValidate.ConvertToTrimInt(sdr["Status"]);
            model.CreateLoginID = PageValidate.ConvertToTrimInt(sdr["CreateLoginID"]);
            model.CreateLoginName = PageValidate.ConvertToTrimString(sdr["CreateLoginName"]);
            model.CreateDate = PageValidate.ConvertToDateTime(sdr["CreateDate"]);
            model.ChangeLoginID = PageValidate.ConvertToTrimInt(sdr["ChangeLoginID"]);
            model.ChangeLoginName = PageValidate.ConvertToTrimString(sdr["ChangeLoginName"]);
            model.ChangeDate = PageValidate.ConvertToDateTime(sdr["ChangeDate"]);
            model.ViewDel = PageValidate.ConvertToTrimInt(sdr["ViewDel"]);
            model.Remark = PageValidate.ConvertToTrimString(sdr["Remark"]);
            return model;
        }
    }
}
